package com.rjyb.student_management_platform.service.Impl;

import com.rjyb.student_management_platform.dao.UserDao;
import com.rjyb.student_management_platform.pojo.Student;
import com.rjyb.student_management_platform.pojo.Teacher;
import com.rjyb.student_management_platform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;


    @Override
    public Map queryStudent(HttpSession session) {
        Student s =(Student) session.getAttribute("student");
        String q = String.valueOf(s.getSid());
        return userDao.queryStudent(q);
    }
    @Override
    public Student ifStudent(String studentId, String name) {
        return userDao.ifStudent(studentId, name);
    }

    @Override
    public boolean addStudent(Student student) {
        return userDao.addStudent(student);
    }



    //修改密码
    @Override
    public String updatapassword(String password, String password1, HttpSession session, Model model) {
        if(session.getAttribute("student")!=null){
            if(password!=null && password.equals(password1)){
                Student student = (Student) session.getAttribute("student");
                long sid = student.getSid();
                userDao.upstudatapassword(password,sid);
                return "student/index";
            }else {
                model.addAttribute("error","修改失败，检查密码");
                return"student/updatastupassword";
            }
        }else if(session.getAttribute("teacher")!=null){
            if(password!=null && password.equals(password1)) {
                Teacher teacher = (Teacher) session.getAttribute("teacher");
                long tid = teacher.getTid();
                userDao.upteadatapassword(password, tid);
                return "teacher/index";
            }else {
                model.addAttribute("error","修改失败，检查密码");
                return"teacher/updatateapassword";
            }
        }
        return null;
    }
}
